package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.GoodsCollection;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.MyFansOrMyConcernsQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void save(User user) {
        //判断邮箱是否已经被注册
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
        if(temp==null){
            //设置默认头像,背景
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("/img/user/bg.jpeg");
            //保存到数据库
            userMapper.insert(user);
        }else{
            throw new RuntimeException("邮箱已被注册!");
        }
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if(user==null){
            throw new RuntimeException("邮箱和密码不匹配!");
        }
        return user;
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    /**
     * 这个当前登录用户关注另一个用户
     * @param concernedId
     */
    public void saveConcerned(Long concernedId,Long userId) {
        if (concernedId == userId){
            throw new RuntimeException("不能自己关注自己哦");
        }
        userMapper.insertConcerned(concernedId,userId);
    }

    /**
     * 通过当前用户的id查询出他关注所有用户的id
     * @param userId
     * @return
     */
    @Override
    public List<Integer> selectByUserIdForConcerneds(Long userId) {
        return userMapper.selectByUserIdForConcerneds(userId);
    }

    /**
     * 通过关注用户id,查询出它所有的fansId
     * @param concernedId
     * @return
     */
    @Override
    public Integer selectFans(Long concernedId) {
        return userMapper.selectByConcernedIdForFans(concernedId);
    }
    /**
     * 通过关注用户id,查询出它所有的关注数
     *      * @param concernedId
     *      * @return
     */
    @Override
    public Integer selectconcerneds(Long concernedId) {
        return userMapper.selectByConcernedIdForConcerned(concernedId);
    }

    /**
     * 取消关注的方法
     * @param userId
     * @param concernedId
     */
    @Override
    public void deleteConcerned(Long userId, Long concernedId) {
        userMapper.deleteConcerned(userId,concernedId);
    }


    @Override
    public void bindWechat(String openId,Long userId) {
        userMapper.bindWx(openId,userId);
    }

    @Override
    public Boolean isBindedWx(String openId) {
        Integer count=userMapper.queryByOpenId(openId);
        return count==0||count==null?false:true;
    }

    @Override
    public User getUserInfoByOpenId(String openId) {
        return userMapper.getUserInfoByOpenId(openId);
    }

    @Override
    public User getUserInfoById(Long id) {

        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsCollection> selectForGoodsCollection() {
        return userMapper.selectForGoodsCollection();
    }

    public User selectById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo listUsersConcerned(MyFansOrMyConcernsQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> myConcerns=userMapper.selectUsersConcerned(qo);
        return new PageInfo(myConcerns);
    }

    @Override
    public PageInfo listUsersFans(MyFansOrMyConcernsQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> myFans=userMapper.selectUsersFans(qo);
        return new PageInfo(myFans);
    }

    /**
     * 升级会员或取消会员
     */
    @Override
    public void becomeOrLostVip(Long id  , Boolean vip) {
        userMapper.becomeOrLostVip(id ,vip);
    }
}
